function [theta_hat_final,theta_var,theta_hat_init,Q]=get_estimates(ests,sims,b_Omega,w_Omega,W,theta_0,options_init,options_final,nobeta,nosigma)

%get simulation draws
b_param_sims_init=cell2mat(sims(1)); w_param_sims_init=cell2mat(sims(5));
b_param_sims_final=cell2mat(sims(2)); w_param_sims_final=cell2mat(sims(6));
b_error_sims_init=cell2mat(sims(3)); w_error_sims_init=cell2mat(sims(7));
b_error_sims_final=cell2mat(sims(4)); w_error_sims_final=cell2mat(sims(8));

%get sample moments
est_m=get_m(ests,W,nobeta,nosigma);

if nobeta==0 && nosigma==0
    %initial estimates
    theta_hat_init=fminunc(@(x)smd(x,est_m,b_param_sims_init,w_param_sims_init,b_error_sims_init,w_error_sims_init,b_Omega,w_Omega,W,nobeta,nosigma),theta_0,options_init);  
    theta_hat_init %#ok
    %final estimates
    [theta_hat_final,Q]=fminunc(@(x)smd(x,est_m,b_param_sims_final,w_param_sims_final,b_error_sims_final,w_error_sims_final,b_Omega,w_Omega,W,nobeta,nosigma),theta_hat_init,options_final);
    theta_hat_final %#ok
end
if nobeta==1 && nosigma==0
    %initial estimates
    theta_hat_init=fminunc(@(x)smd([x(1:2);0;x(3:7);0;x(8:12)],est_m,b_param_sims_init,w_param_sims_init,b_error_sims_init,w_error_sims_init,b_Omega,w_Omega,W,nobeta,nosigma),theta_0,options_init);   
    theta_hat_init %#ok
    %final estimates
    [theta_hat_final,Q]=fminunc(@(x)smd([x(1:2);0;x(3:7);0;x(8:12)],est_m,b_param_sims_final,w_param_sims_final,b_error_sims_final,w_error_sims_final,b_Omega,w_Omega,W,nobeta,nosigma),theta_hat_init,options_final);
    theta_hat_final %#ok
end
if nobeta==0 && nosigma==1
    %initial estimates
    theta_hat_init=fminunc(@(x)smd([x(1:3);0;x(4:8);0;x(9:11);0],est_m,b_param_sims_init,w_param_sims_init,b_error_sims_init,w_error_sims_init,b_Omega,w_Omega,W,nobeta,nosigma),theta_0,options_init);   
    theta_hat_init %#ok
    %final estimates
    [theta_hat_final,Q]=fminunc(@(x)smd([x(1:3);0;x(4:8);0;x(9:11);0],est_m,b_param_sims_final,w_param_sims_final,b_error_sims_final,w_error_sims_final,b_Omega,w_Omega,W,nobeta,nosigma),theta_hat_init,options_final);
    theta_hat_final %#ok
end
if nobeta==1 && nosigma==1
    %initial estimates
    theta_hat_init=fminunc(@(x)smd([x(1:2);0;0;x(3:6);0;0;x(7:9);0],est_m,b_param_sims_init,w_param_sims_init,b_error_sims_init,w_error_sims_init,b_Omega,w_Omega,W,nobeta,nosigma),theta_0,options_init);   
    theta_hat_init %#ok
    %final estimates
    [theta_hat_final,Q]=fminunc(@(x)smd([x(1:2);0;0;x(3:6);0;0;x(7:9);0],est_m,b_param_sims_final,w_param_sims_final,b_error_sims_final,w_error_sims_final,b_Omega,w_Omega,W,nobeta,nosigma),theta_hat_init,options_final);
    theta_hat_final %#ok
end

%SEs
if nobeta==0 && nosigma==0
    V=get_v(theta_hat_final,est_m,b_param_sims_final,w_param_sims_final,b_error_sims_final,w_error_sims_final,b_Omega,w_Omega,W,nobeta,nosigma);
    [~,~,~,~,~,~,G]=lsqnonlin(@(x)get_g(x,est_m,b_param_sims_final,w_param_sims_final,b_error_sims_final,w_error_sims_final,b_Omega,w_Omega,W,nobeta,nosigma),theta_hat_final,[],[],optimset('MaxIter',0,'TolFun',1000000000));
    G_big=full(G);
end
if nobeta==1 && nosigma==0
    V=get_v([theta_hat_final(1:2);0;theta_hat_final(3:7);0;theta_hat_final(8:12)],est_m,b_param_sims_final,w_param_sims_final,b_error_sims_final,w_error_sims_final,b_Omega,w_Omega,W,nobeta,nosigma);
    [~,~,~,~,~,~,G]=lsqnonlin(@(x)get_g([x(1:2);0;x(3:7);0;x(8:12)],est_m,b_param_sims_final,w_param_sims_final,b_error_sims_final,w_error_sims_final,b_Omega,w_Omega,W,nobeta,nosigma),theta_hat_final,[],[],optimset('MaxIter',0,'TolFun',1000000000));
    G_big=full(G);    
end
if nobeta==0 && nosigma==1
    V=get_v([theta_hat_final(1:3);0;theta_hat_final(4:8);0;theta_hat_final(9:11);0],est_m,b_param_sims_final,w_param_sims_final,b_error_sims_final,w_error_sims_final,b_Omega,w_Omega,W,nobeta,nosigma);
    [~,~,~,~,~,~,G]=lsqnonlin(@(x)get_g([x(1:3);0;x(4:8);0;x(9:11);0],est_m,b_param_sims_final,w_param_sims_final,b_error_sims_final,w_error_sims_final,b_Omega,w_Omega,W,nobeta,nosigma),theta_hat_final,[],[],optimset('MaxIter',0,'TolFun',1000000000));
    G_big=full(G);    
end
if nobeta==1 && nosigma==1
    V=get_v([theta_hat_final(1:2);0;0;theta_hat_final(3:6);0;0;theta_hat_final(7:9);0],est_m,b_param_sims_final,w_param_sims_final,b_error_sims_final,w_error_sims_final,b_Omega,w_Omega,W,nobeta,nosigma);
    [~,~,~,~,~,~,G]=lsqnonlin(@(x)get_g([x(1:2);0;0;x(3:6);0;0;x(7:9);0],est_m,b_param_sims_final,w_param_sims_final,b_error_sims_final,w_error_sims_final,b_Omega,w_Omega,W,nobeta,nosigma),theta_hat_final,[],[],optimset('MaxIter',0,'TolFun',1000000000));
    G_big=full(G);    
end
theta_var=inv(G_big'*(V\G_big));
